//
//  AddAddressViewController.m
//  AnnCar_user
//
//  Created by LiBenCan on 17/10/26.
//  Copyright © 2017年 LiBenCan. All rights reserved.
//

#import "AddAddressViewController.h"
#import "searchView.h"
@interface AddAddressViewController ()<MAMapViewDelegate,AMapLocationManagerDelegate,UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)MAMapView *mapView;//地图
@property (nonatomic,strong)MAPointAnnotation *annotationPoint;//大头针
@property (nonatomic,strong)CanLab *tureLab;//确认按钮
@property (nonatomic,strong)AMapLocationManager *locationManager;
@property (nonatomic, assign) CGFloat Mylatitude;///纬度
@property (nonatomic, assign) CGFloat Mylongitude;///经度
@property (nonatomic, strong) searchView *searchV;///经度
@property (nonatomic,strong)UITableView *tableV;
@property (nonatomic,strong)BaseMBView *baseBgView;//蒙板
@end

@implementation AddAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initTitleWithTitle:@"位置选择"];
    [self initLeftBarButtonWithImage:@"back"];
    [self.view addSubview:self.mapView];
    [self.view addSubview:self.tureLab];
    [self.locationManager startUpdatingLocation];
    [self.view addSubview:self.searchV];
}

//搜索＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊

//创建TableView
-(UITableView *)tableV
{
    if (!_tableV) {
        _tableV = [[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREENW,0) style:UITableViewStyleGrouped];
        _tableV.backgroundColor = backColor;
        _tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableV.showsVerticalScrollIndicator = NO;
        _tableV.rowHeight = 35*screenW_6;
        _tableV.delegate = self;
        _tableV.dataSource = self;
    }
    _tableV.frame = CGRectMake(0,0, SCREENW, 210*screenW_6);
    return _tableV;
}

//搜索View
-(searchView *)searchV
{
    if (!_searchV) {
        _searchV = [[searchView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, 45*screenW_6)];
        _searchV.textF.delegate = self;
        [_searchV.textF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return _searchV;
}
//编辑开始添加这层 显示tableView
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    _baseBgView = [[BaseMBView alloc]initWithFrame:CGRectMake(0,45*screenW_6,SCREENW,SCREENH)];
    [_baseBgView showView:self];
    [_baseBgView addSubview:self.tableV];
    __weak typeof(self)weakSelf = self;
    _baseBgView.dissBlock = ^(){
        [weakSelf.view endEditing:YES];
        weakSelf.tableV.frame = CGRectMake(0, 0, SCREENW, 0);
    };
}
//编辑结束 去除遮盖 移开tableV
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    self.tableV.frame = CGRectMake(0, 0, SCREENW, 0);
    [_baseBgView removeFromSuperview];
}
//搜索输入处理
-(void)textFieldDidChange:(UITextField *)textField
{
    
}

#pragma Mark TableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
//区头高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.000001;
}
//区尾高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.000001;
}
//加载Cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }
    cell.textLabel.text = indexPath.row == 0?@"搜索历史":@"光谷广场";
    cell.textLabel.textColor = indexPath.row == 0?TextBlack:TextGray;
    [cell addSubview:[ToolViewVC addCellLineLab:CGRectMake(15*screenW_6, 34*screenW_6, SCREENW-15*screenW_6,1*screenW_6)]];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.tableV.frame = CGRectMake(0, 0, SCREENW, 0);
    [_baseBgView removeFromSuperview];
}
//高德地图＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊

-(MAMapView *)mapView
{
    if (!_mapView)
    {
        _mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 45*screenW_6, SCREENW, SCREENH-90*screenW_6)];
        [_mapView setDelegate:self];
        _mapView.zoomLevel = 15;
        //精度圈
        MAUserLocationRepresentation *r = [[MAUserLocationRepresentation alloc] init];
        r.showsAccuracyRing = NO;///精度圈是否显示，默认YES
        [self.mapView updateUserLocationRepresentation:r];
        
        UITapGestureRecognizer *mapTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(mTapPress:)];
        [self.mapView addGestureRecognizer:mapTap];
    }
    return _mapView;
}

//定位
- (AMapLocationManager *)locationManager {
    if (!_locationManager) {
        _locationManager = [[AMapLocationManager alloc] init];
        _locationManager.delegate = self;
        [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
        self.locationManager.locationTimeout =10;
        self.locationManager.reGeocodeTimeout = 10;
        _locationManager.allowsBackgroundLocationUpdates = YES;
        _locationManager.pausesLocationUpdatesAutomatically = NO;
    }
    return _locationManager;
}

//持续定位
- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location reGeocode:(AMapLocationReGeocode *)reGeocode
{
    NSLog(@"location:{lat:%f; lon:%f; accuracy:%f}", location.coordinate.latitude, location.coordinate.longitude, location.horizontalAccuracy);
    if (reGeocode)
    {
        NSLog(@"reGeocode:%@", reGeocode);
    }
    if (!_annotationPoint) {
        _annotationPoint = [[MAPointAnnotation alloc] init];
    }
    _annotationPoint.coordinate = location.coordinate;
    [self.mapView addAnnotation:_annotationPoint];
    self.mapView.centerCoordinate = location.coordinate;
    [_locationManager stopUpdatingLocation];
}
//点击地图事件
- (void)mTapPress:(UIGestureRecognizer*)gestureRecognizer {
    
    //点击的某点在地图控件中的位置
    CGPoint touchPoint = [gestureRecognizer locationInView:self.mapView];
    //该点的经纬度
    CLLocationCoordinate2D touchMapCoordinate =
    [self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];
    NSLog(@"touching %f,%f",touchMapCoordinate.latitude,touchMapCoordinate.longitude);
    if (!_annotationPoint) {
        _annotationPoint = [[MAPointAnnotation alloc] init];
    }
    _annotationPoint.coordinate = touchMapCoordinate;
    [self.mapView addAnnotation:_annotationPoint];
}

//确认 回调经纬度
-(CanLab *)tureLab
{
    if (!_tureLab) {
        _tureLab = [[CanLab alloc]initWithFrame:CGRectMake(0,SCREENH-64-45*screenW_6,SCREENW,45*screenW_6) textAligment:1 font:16*screenW_6 textColor:@"#ffffff" text:@"确认位置"];
        _tureLab.backgroundColor = blueColor;
        _tureLab.block = ^(CanLab *lab)//保存点击事件
        {
            
        };
    }
    return _tureLab;
}
@end
